{
 "nbformat": 4,
 "nbformat_minor": 2,
 "metadata": {
  "language_info": {
   "name": "python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "version": "3.7.5"
  },
  "orig_nbformat": 2,
  "file_extension": ".py",
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "version": 3
 },
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 14. 最长公共前缀"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "outputs": [],
   "source": [
    "编写一个函数来查找字符串数组中的最长公共前缀。\n",
    "\n",
    "如果不存在公共前缀，返回空字符串 \"\"。\n",
    "\n",
    "示例 1:\n",
    "\n",
    "输入: [\"flower\",\"flow\",\"flight\"]\n",
    "输出: \"fl\"\n",
    "示例 2:\n",
    "\n",
    "输入: [\"dog\",\"racecar\",\"car\"]\n",
    "输出: \"\"\n",
    "解释: 输入不存在公共前缀。\n",
    "说明:\n",
    "\n",
    "所有输入只包含小写字母 a-z 。\n",
    "\n",
    "来源：力扣（LeetCode）\n",
    "链接：https://leetcode-cn.com/problems/longest-common-prefix\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": "as\n"
    }
   ],
   "source": [
    "class Solution(object):\n",
    "    def longestCommonPrefix(self, strs):\n",
    "        \"\"\"\n",
    "        :type strs: List[str]\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        result = \"\"\n",
    "        if len(strs) == 0:\n",
    "            return result\n",
    "        # 取出最短的字符串\n",
    "        flag_str = min(strs, key=len)\n",
    "        # 原始列表中去除这个，避免自己和自己判断\n",
    "        strs.remove(flag_str)\n",
    "        # 循环每一个最短字符串的字符，与列表的其他字符串进行比较\n",
    "        for i, each_char in enumerate(flag_str):\n",
    "            for each in strs:\n",
    "            \t# 只要发现不等就马上退出\n",
    "                if each[i] != each_char:\n",
    "                    return result\n",
    "            result += each_char\n",
    "        return result\n",
    "        \n",
    "if __name__ == \"__main__\":\n",
    "    Sol = Solution()\n",
    "    end = Sol.longestCommonPrefix([\"asb\",\"asf\",\"asd\"])\n",
    "    print(end)"
   ]
  }
 ]
}